package com.example.xuanke.Controller;


import com.example.xuanke.Entity.CourseEntity;
import com.example.xuanke.Entity.StudentCourseEntity;
import com.example.xuanke.Entity.StudentEntity;
import com.example.xuanke.commton.Wrapper;
import com.example.xuanke.service.CourseSerivce;
import com.example.xuanke.service.StudentCourseSerivce;
import com.example.xuanke.service.StudentSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentSerivce studentSerivce;
    @Autowired
    private CourseSerivce courseSerivce;
    @Autowired
    StudentCourseSerivce studentCourseSerivce;
    
    @PostMapping("/register")
    public Wrapper<?> addStudent(@RequestBody StudentEntity student) {
        return studentSerivce.addUser(student);
    }
    @PostMapping("/login")
public Wrapper<Boolean> login(@RequestBody StudentEntity loginRequest) {
    // 可能这里获取参数的方式有问题
    String studentNo = loginRequest.getStudentNo();
    String password = loginRequest.getPassword();
    
    return studentSerivce.login(studentNo, password);
}
@GetMapping("getAllCourse")
public Wrapper<List<CourseEntity>> getAllCourse(){
        return courseSerivce.getAllCourse();
}
@PostMapping("/chooseCourse")
public Wrapper<Boolean> chooseCourse(@RequestBody Map<String, Object> params) {
    // 从Map中提取参数并转换为正确的类型
    String studentNoStr = (String) params.get("studentNo");
    Integer courseIdInt = (Integer) params.get("courseId");
    
    // 确保参数不为空
    if (studentNoStr == null || courseIdInt == null) {
        return new Wrapper<>(500, "学号或课程ID不能为空", false);
    }
    
    // 处理类型转换（如果需要）
    Long courseId = courseIdInt.longValue();
    

    
    return studentCourseSerivce.studentCourseAdd(studentNoStr, courseId);
}
    @PostMapping("/deleteCourse")
    public Wrapper<Boolean>deleteCourse(@RequestBody StudentCourseEntity studentCourse){
        String studentNo = studentCourse.getStudentNo();
        Long courseId = studentCourse.getCourseId();

        if (studentNo == null || courseId == null) {
            return new Wrapper<>(500, "学号或课程ID不能为空", false);
        }

        // 调用service层的退课方法
        return studentCourseSerivce.studentCourseDelete(studentNo, courseId);
    }

    @PostMapping("/list")
    public Wrapper<?> getStudentCourses(@RequestBody StudentEntity studentCourse){
        String studentNo = studentCourse.getStudentNo();
        return studentCourseSerivce.getStudentCourses(studentNo);
    }
}



